package com.anuprayog.flyinghelicopter;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

/**
 * @author amit
 * This activity is where the user is redirected after the game gets over
 * showing the score the user made
 */
public class GameOverActivity extends Activity {

        private TextView textview;
       
        public static String SOUND_STATE = "";
		
		public static String EXTRA_SCORE = "";
		public static String HIGHSCORE = "";
		
		public String score;
		public String soundState;
       
        public int OLD_HIGH_SCORE=0;
       
        public int NEW_HIGH_SCORE;
       
         private String filename = "MyFile.txt";
         private String filepath = "MyFileStorage";
         File myInternalFile;
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_game_over);
               
                //code added by manoj chand on 23 july 2013
                ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
                File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
                myInternalFile = new File(directory , filename);
               
                textview = (TextView) findViewById(R.id.textView1);
               
                Intent i = getIntent();
               
                score = i.getStringExtra(MainGamePanel.HIGH_SCORE);
               
                soundState = i.getStringExtra(MainGamePanel.SOUND_OFF_STRING);
                
                textview.setText("Your Score:" + score);
               
                textview.setTextColor(Color.YELLOW);
               
                //textview.setTextSize(40);
               
                //call to get high score
                OLD_HIGH_SCORE = getHighScore();
               
                try {
                        NEW_HIGH_SCORE = Integer.parseInt(score);
                } catch (RuntimeException e){
                        NEW_HIGH_SCORE = 0;
                }
               
                if (NEW_HIGH_SCORE > OLD_HIGH_SCORE){
                        OLD_HIGH_SCORE = NEW_HIGH_SCORE;
                }
               
                //call to set high score
                setHighScore(OLD_HIGH_SCORE);
                Intent intent = new Intent(this, HighActivity.class);
                //intent.putExtra(EXTRA_SCORE, score);
               
                //HIGHSCORE = score;
               
        }
       
        //function created by manoj chand to set high score in internal storage
        public void setHighScore(int score){
                try {
                        String strScore = score+"";
                    FileOutputStream fos = new FileOutputStream(myInternalFile);
                    fos.write(strScore.getBytes());
                    fos.close();
                   } catch (IOException e) {
                    e.printStackTrace();
                   }
        }

        //function created by manoj chand to get high score from internal storage
        public int getHighScore(){
                String myData="0";
                   try {
                            FileInputStream fis = new FileInputStream(myInternalFile);
                            DataInputStream in = new DataInputStream(fis);
                            BufferedReader br = new BufferedReader(new InputStreamReader(in));
                            String strLine;
                            while ((strLine = br.readLine()) != null) {
                             myData = strLine;
                            }
                            in.close();
                           } catch (IOException e) {
                            e.printStackTrace();
                           }
                   if(myData != null && myData.trim().equals(""))
                           myData="0";
                   return Integer.parseInt(myData);
                }
       
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.game_over, menu);
                return true;
        }
       
        public void goBack(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(SOUND_STATE, soundState);
	    startActivity(intent);
        finish();
    }
        
        @Override
    	public void onBackPressed() {

        	Intent intent = new Intent(this, MainActivity.class);
        	intent.putExtra(SOUND_STATE, soundState);
		    startActivity(intent);
            finish();

    	}

}

